package edu.cuny.baruch.cis9590.library.database;

import java.util.ArrayList;
import java.util.List;

import edu.cuny.baruch.cis9590.library.entities.Book;
import edu.cuny.baruch.cis9590.library.entities.Borrow;

public final class BookDAO extends LibraryDAO<Book> {
    
    @Override
    protected Class<Book> getEntityClass() {
        return Book.class;
    }
    
    public List<Book> getByAuthor(String author) {
        return query("from Book where author like '%" + author + "%'");
    }
    
    public List<Book> getByTitle(String title) {
        return query("from Book where title like '%" + title + "%'");
    }
    
    public List<Book> getByCallNumber(String callNumber) {
        return query("from Book where callNumber like '%" + callNumber + "%'");
    }
    
    public List<Book> getByPatron(int patronId) {
        List<Book> books = new ArrayList<Book>();
        for (Borrow borrow : (List<Borrow>) query("from Borrow where patron_id = " + patronId)) {
            books.add(getById(borrow.getBookId()));
        }
        return books;
    }
    
    public static void main(String[] args) {
        System.out.println(new BookDAO().getByPatron(1));
    }
}
